local skel = fk.CreateSkill {
  name = "emo__qijian",
}

Fk:loadTranslationTable{
  ["emo__qijian"] = "起剑",
  [":emo__qijian"] = "出牌阶段限一次，你可以选一项：①将你两张牌明置且令你使用下张牌伤害+1，然后选择者下家亦须抉择；②令你视为使用/重置此技能视为对你使用【决斗】。",

  ["#emo__qijian"] = "起剑：请明置2张牌；或视为使用【决斗】",
  ["@emo__qijian"] = "起剑+",
  ["emo__qijian_show"] = "明置2张牌",
  ["emo__qijian_duel"] = "使用决斗",
  ["#emo__qijian_show"] = "明置%src 2张牌令其下张牌伤害+1",
  ["#emo__qijian_duel"] = "令 %src 视为使用【决斗】",
  ["#emo__qijian_reset"] = "重置“起剑”视为对 %src 用【决斗】",
  ["#emo__qijian-show"] = "选择2张明置！！",

  ["$emo__qijian1"] = "",
  ["$emo__qijian2"] = "",
}

local DIY = Pcall(require, "packages/diy_utility/diy_utility")

--- 令from明置player两张手牌
---@param player ServerPlayer
---@param from ServerPlayer
local function showMeCards(player, from)
  local room = player.room
  local hide = table.filter(player:getCardIds("h"), function (id)
    return not table.contains(DIY.getShownCards(player), id)
  end)
  if #hide < 2 then return end
  local cards = {}
  if from == player then
    cards = room:askToCards(player, {
      skill_name = skel.name, pattern = tostring(Exppattern{ id = hide }), min_num = 2, max_num = 2,
      prompt = "#emo__qijian-show", cancelable = false,
    })
  else
    local data = {
      to = player.id,
      min = 2,
      max = 2,
      skillName = skel.name,
      prompt = "#emo__qijian-show",
    }
    local cards_data = { { "$Hand", hide } }
    local visible_data = {}
    for _, id in ipairs(hide) do
      visible_data[tostring(id)] = from:cardVisible(id)
    end
    data.visible_data = visible_data
    local poxiParams = { ---@type AskToPoxiParams
      poxi_type = "AskForCardsChosen",
      data = cards_data,
      extra_data = data,
      cancelable = false
    }
    cards = room:askToPoxi(from, poxiParams)
  end
  room:addPlayerMark(player, "@emo__qijian", 1)
  DIY.showCards(player, cards)
end

skel:addEffect("active", {
  anim_type = "offensive",
  prompt = "#emo__qijian",
  interaction = function(self, player)
    return UI.ComboBox { choices = {"emo__qijian_show", "emo__qijian_duel"} }
  end,
  card_filter = function (self, player, to_select, selected)
    return #selected < 2 and self.interaction.data == "emo__qijian_show" and table.contains(player:getCardIds("h"), to_select)
    and not table.contains(DIY.getShownCards(player), to_select)
  end,
  target_filter = function (self, player, to_select, selected)
    if self.interaction.data == "emo__qijian_duel" then
      local card = Fk:cloneCard("duel")
      card.skillName = skel.name
      return card.skill:targetFilter(player, to_select, selected, {}, card)
    end
  end,
  can_use = function(self, player)
    return player:usedSkillTimes(skel.name, Player.HistoryPhase) == 0
  end,
  feasible = function (self, player, selected, selected_cards)
    if self.interaction.data == "emo__qijian_show" then
      return #selected_cards == 2
    else
      return #selected > 0
    end
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local choice = self.interaction.data
    if choice == "emo__qijian_duel" then
      room:useVirtualCard("duel", nil, player, effect.tos, skel.name)
    else
      room:addPlayerMark(player, "@emo__qijian", 1)
      DIY.showCards(player, effect.cards)
      local to = player
      while player:isAlive() do
        local nextp = to:getNextAlive()
        if nextp == to then break end
        to = nextp ---@type ServerPlayer
        local hide = table.filter(player:getCardIds("h"), function (id)
          return not table.contains(DIY.getShownCards(player), id)
        end)
        local all_choices = table.map({"#emo__qijian_show", "#emo__qijian_duel", "#emo__qijian_reset"}, function (s)
          return s..":" .. player.id
        end)
        local choices = table.simpleClone(all_choices)
        local duel = Fk:cloneCard("duel")
        duel:setVSPattern(skel.name, player, ".")
        if not to:canUseTo(duel, player) then
          table.remove(choices, 3)
        end
        if not player:canUse(duel) then table.remove(choices, 2) end
        if #hide < 2 then
          table.remove(choices, 1)
        end
        if #choices == 0 then break end
        choice = room:askToChoice(to, {
          skill_name = skel.name, choices = choices, all_choices = all_choices,
        })
        if choice:startsWith("#emo__qijian_show") then
          showMeCards(player, to)
        elseif choice:startsWith("#emo__qijian_reset") then
          player:setSkillUseHistory(skel.name, 0, Player.HistoryPhase)
          room:useVirtualCard("duel", nil, to, player, skel.name)
          break
        else
          room:askToUseVirtualCard(player, {
            skill_name = skel.name, name = "duel", cancelable = false,
          })
          break
        end
      end
    end
  end,
})

skel:addEffect(fk.CardUsing, {
  can_refresh = function (self, event, target, player, data)
    return player == target and player:getMark("@emo__qijian") > 0
  end,
  on_refresh = function (self, event, target, player, data)
    data.additionalDamage = (data.additionalDamage or 0) + player:getMark("@emo__qijian")
    player.room:setPlayerMark(player, "@emo__qijian", 0)
  end,
})

return skel
